/* 
 * GrayRabbit Cms基于J2EE架构内容管理系统
 * Copyright © GrayRabbit Co., Ltd. All rights reserved.
 * Department：运营部
 * 更多信息请访问：
 * http://code.google.com/p/gray-rabbit-cms/
 */
package org.grayrabbit.cms.dao.search.impl;

import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.grayrabbit.cms.dao.search.HotKeywordDao;
import org.grayrabbit.cms.entity.HotKeyword;
import org.grayrabbit.cms.vo.search.HotKeywordBean;
import org.springframework.stereotype.Repository;

import org.grayrabbit.dao.impl.GenericHibernateDao;
import org.grayrabbit.dao.util.Page;
import org.grayrabbit.dao.util.QueryCondition;

/**
 * <b>HotKeyword</b> dao implementation 





 * @version 1.0，创建时间：2011-05-16 14:51:27 
 */
@Repository
public class HotKeywordDaoImpl extends GenericHibernateDao<HotKeyword, String>
		implements HotKeywordDao {

	/**
	 * HQL主体语句
	 */
	public static final String MAIN_HQL = "From HotKeyword as obj";
	/**
	 * HQL中count统计hql片段
	 */
	public static final String COUNT_HQL = "Select Count(obj) ";

	/**
	 * 根据bean组装查询条件
	 * @see HotKeywordDao#assembly(HotKeywordBean)
	 */
	public QueryCondition assembly(HotKeywordBean bean) {
		QueryCondition qc = new QueryCondition(MAIN_HQL);
		transNameQuery(bean,qc);
		return qc;
	}

	/**
	 * 根据bean组装统计查询条件
	 * @see HotKeywordDao#assemblyCount(HotKeywordBean)
	 */
	public QueryCondition assemblyCount(HotKeywordBean bean) {
		QueryCondition qc = new QueryCondition(COUNT_HQL + MAIN_HQL);
		transNameQuery(bean,qc);
		return qc;
	}

	/**
	 * @see HotKeywordDao#count(HotKeywordBean)
	 */
	public int count(HotKeywordBean bean) {
		return ((Long)iterator(assemblyCount(bean)).next()).intValue();
	}

	/**
	 * 根据bean组装查询条件(名称参数)
	 * @see HotKeywordDao#assemblyNameQuery(HotKeywordBean)
	 */
	public QueryCondition assermblyNameQuery(HotKeywordBean bean) {
		QueryCondition qc = new QueryCondition(MAIN_HQL);
		transNameQuery(bean, qc);
		return qc;
	}

	/**
	 * 根据bean组装统计查询条件(名称参数)
	 * @see HotKeywordDao#assemblyNameQueryCount(HotKeywordBean)
	 */
	public QueryCondition assemblyNameQueryCount(HotKeywordBean bean) {
		QueryCondition qc = new QueryCondition(COUNT_HQL + MAIN_HQL);
		transNameQuery(bean,qc);
		return qc;
	}

	/**
	 * 填充分页组件
	 * @see HotKeywordDao#findByPage(Page, HotKeywordBean)
	 */
	public List<HotKeyword> findByPage(Page<HotKeyword> page,
			HotKeywordBean bean) {
		QueryCondition qc = assembly(bean);
		qc.setMaxResults(page.getPageSize());
		qc.setOffset(page.getOffset());
		return findByQc(qc);
	}
	/**
	 * 检查关键字唯一性
	 * @param bean
	 * @param qc
	 */
	public Boolean checkUnique(String hotKeywordName, String hotKeywordId) {
		boolean unique = false;
		List<HotKeyword> keyList = null;
		QueryCondition qc = new QueryCondition(MAIN_HQL);
		if(StringUtils.isNotBlank(hotKeywordName)){
			qc.add(" And obj.hotKeywordName like ? ", hotKeywordName);
			keyList = findByQc(qc);
		}
		if(StringUtils.isNotBlank(hotKeywordId)){
			if((keyList != null) && (!keyList.isEmpty())){
				if((!keyList.get(0).getHotKeywordId().equals(hotKeywordId)) || (keyList.size() >= 2)){
					unique = true;
				}
			}
		}else {
			if((keyList != null) && (!keyList.isEmpty())){
				unique = true;//不可用
			}
		}
		return unique;
	}
	/**
	 * 检查文件前缀唯一性
	 * @param bean
	 * @param qc
	 */
	public Boolean checkFilenameUnique(String frontFilename, String hotKeywordId) {
		boolean unique = false;
		List<HotKeyword> keyList = null;
		QueryCondition qc = new QueryCondition(MAIN_HQL);
		if(StringUtils.isNotBlank(frontFilename)){
			qc.add(" And obj.frontFilename like ? ", frontFilename);
			keyList = findByQc(qc);
		}
		if(StringUtils.isNotBlank(hotKeywordId)){
			if((keyList != null) && (!keyList.isEmpty())){
				if((!keyList.get(0).getHotKeywordId().equals(hotKeywordId)) || (keyList.size() >= 2)){
					unique = true;
				}
			}
		}else {
			if((keyList != null) && (!keyList.isEmpty())){
				unique = true;//不可用
			}
		}
		return unique;
	}

	// 业务查询主要逻辑代码
	private void transNameQuery(HotKeywordBean bean, QueryCondition qc) {
		if (bean != null) {
			if (bean.getSearchNum() != null) {
				qc.add("And obj.searchNum like ? ", bean.getSearchNum() + "%");
			}
			if (bean.getIsGenerate() != null) {
				qc.add("And obj.isGenerate like ? ", bean.getIsGenerate() + "%");
			}
			if (bean.getFrontFilename() != null) {
				qc.add("And obj.frontFilename like ? ", bean.getFrontFilename()
						+ "%");
			}
			if (bean.getHotType() != null) {
				qc.add("And obj.hotType like ? ", bean.getHotType() + "%");
			}
			if (bean.getHotKeywordName() != null) {
				qc.add("And obj.hotKeywordName like ? ",
						"%" + bean.getHotKeywordName() + "%");
			}

		}
	}

	public HotKeyword findByKeyWord(String key) {
		if(StringUtils.isBlank(key)) return null;
		QueryCondition qc = new QueryCondition(MAIN_HQL);
		qc.add(" And obj.hotKeywordName = ? ", key);
		qc.appenOrderBy(" obj.firstTime  asc ");
		List<HotKeyword> list = findByQc(qc);
		if(list == null || list.isEmpty()) return null; 
		return list.get(0);
	}

}